package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 
 * 创建日期: 2022/05/18 22:18
 * @author wangyonghong
 */
 @Service
public class IProductServiceImpl  implements IProductService {

    @Autowired
    ItemMapper itemMapper;


    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ContentConverter contentConverter;
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();

        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = productConverter.itemDetailDto(item);
            productDetailDto.setProductImageSmall(item.getImages());
            productDetailDto.setProductImageBig(item.getImageBig());
            productDetailDto.setDetail(itemDesc.getItemDesc());
            productDetailResponse.setProductDetailDto(productDetailDto);
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(productDetailResponse, e);
        }
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();
        try {
            request.requestCheck();
            //  List<ProductDto> list =new LinkedList<>();

            Integer size = request.getSize();
            Integer page = request.getPage();


            Integer priceGt = request.getPriceGt();
            Integer priceLte = request.getPriceLte();
            String sort = request.getSort();



            List<Item> items1=null;
            if(priceGt==null||priceGt.equals("")){priceGt=0;}
            if(priceLte==null||priceLte.equals("")){priceLte=999999;}
            PageHelper.startPage(page, size);
            Example example=new Example(Item.class);
            example.createCriteria().orBetween("price",priceGt,priceLte);
            items1=itemMapper.selectByExample(example);

            if(sort.equals("1")){
                PageHelper.startPage(page, size);
                Example example1=new Example(Item.class);
                example1.orderBy("price").asc();
                example1.createCriteria().orBetween("price",priceGt,priceLte);
                items1=itemMapper.selectByExample(example1);
            }
            if(sort.equals("-1")){
                PageHelper.startPage(page, size);
                Example example2=new Example(Item.class);
                example2.orderBy("price").desc();
                example2.createCriteria().orBetween("price",priceGt,priceLte);
                items1=itemMapper.selectByExample(example2);
            }

            PageInfo<Item> pageInfo = new PageInfo<>(items1);
            long total = pageInfo.getTotal();
            List<ProductDto> productDtos = productConverter.items2Dto(items1);
            for (ProductDto productDto : productDtos) {
                productDto.setPicUrl(getImageBig(productDto.getPicUrl()));
            }



            allProductResponse.setProductDtoList(productDtos);
            allProductResponse.setTotal(total);
          allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
          allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }

        return allProductResponse;
    }

















    public String getImageBig(String image){
        if (image != null && !"".equals(image)) {
            String[] strings = image.split(",");
            return strings[0];
        }
        return null;
    }
    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();
        Set<PanelDto> panelDtos = new HashSet<PanelDto>();

//板块ID待获取
//        List<Panel> panels = panelMapper.selectPanelContentById(6);
//        Panel panel = panels.get(0);
        try {

            PanelDto panelDto = contentConverter.panel2Dto(panelMapper.selectPanelContentById(6).get(0));
            //  List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(6);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentMapper.selectPanelContentAndProductWithPanelId(6));
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDtos.add(panelDto);
            recommendResponse.setPanelContentItemDtos(panelDtos);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);
        }
        return recommendResponse;
    }

    @Override
    public AllItemResponse getAllItems() {
        AllItemResponse allItemResponse = new AllItemResponse();


        try {
            List<Item> items = itemMapper.selectAll();

            List<ItemDto> itemDtos = productConverter.items2ItemDtos(items);
            allItemResponse.setAllItems(itemDtos);
            allItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(allItemResponse, e);
        }
        return allItemResponse;


    }
}
